<template>
  <div>
    <el-form :inline="true" :model="user" class="demo-form-inline"  :rules="rules" ref="ruleForm">
      <el-form-item label="账户" prop="username">
        <el-input v-model="user.username" placeholder="账户"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" >
        <el-input v-model="user.password" placeholder="密码" show-password></el-input>
      </el-form-item>

      <el-form-item label="验证码" prop="code">
        <el-input v-model="user.code" placeholder="验证码" ></el-input>
        <el-image :src="codeURl" @click="getCode"></el-image>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login('ruleForm')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  name: "LoginView",
  data() {
    return {
      user:{},
      codeURl:'',
      rules: {
        username: [
          {required: true, message: '请输入账户', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
        ],
      }
    }
  }, methods: {
    login(formName){

      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post("http://localhost:9999/auth/login",this.user).then(res=>{
            if(res.data.code ==200){
              this.$message.success(res.data.msg);
              sessionStorage.setItem("token",res.data.data);//存储token
              this.$router.push("/goods");//跳转到商品列表
            }else{
              this.$message.error(res.data.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });


    },

   getCode(){
      let t= new Date().getTime();
      let uuid =sessionStorage.getItem("uid");
      this.user.uuid=uuid;
      this.codeURl='http://localhost:9999/auth/createCode?t='+t+"&uuid="+uuid;
   }
  },
  created() {
    this.getCode();
  }
}
</script>

<style scoped>

</style>